<?php

namespace app\apiadmin\service\user;

use think\facade\Db;
use think\facade\Log;
use app\common\utils\Res;


class UserService
{
    
    public static function getWxLogin($param){
        if (empty($param['redirect_uri'])) {
            return Res::error('缺少参数');
        }
        if (strpos($param['redirect_uri'], '/liveapp.banxued.com') !== false) {
            //正式私域
            return self::getLoginBuild($param);
        } elseif (strpos($param['redirect_uri'], '/siyuapptest.banxued.com') !== false) {
            //预发布私域
            return self::getLoginTestPre($param);
        } elseif (strpos($param['redirect_uri'], '/siyuapp.zhiked.com') !== false) {
            //测试私域
            return self::getLoginTest($param);
        } elseif (strpos($param['redirect_uri'], '/ajliveapp.banxued.com') !== false) {
            //AJ私域
            return self::getLoginClientAj($param);
        } elseif (strpos($param['redirect_uri'], '/lyzliveapp.banxued.com') !== false) {
            //李一舟私域
            return self::getLoginClientLiyz($param);
        } else {
            //未找到对应域名-走正式私域
            return self::getLoginBuild($param);
        }
    }

    /**
     * @author: hao
     * @Description: 预测试私域微信登录
     * @Date: 2024-08-17 17:17:55
     */
    public static function getLoginTestPre($param){
        $data = [];
        $openid = $param['oauth']['openid'];
        $nickname = $param['info']['nickname'];
        $headimgurl = $param['info']['headimgurl'];
        $unionid = $param['info']['unionid'];

        //信息入库
        $where[] = ['unionid', '=', $unionid];
        $where[] = ['del', '=', 0];
        $if_exist = DB::connect('test_pre')->table('xh_siyu_user')->where($where)->find();
        if (empty($if_exist)) {
            $data['openid'] = $openid;
            $data['unionid'] = $unionid;
            $data['union_md5'] = md5($unionid);
            $data['nickname'] = $nickname;
            $data['wx_avatarurl'] = $headimgurl;
            $data['type'] = 0;
            $data['status'] = 1;
            $data['status_ch'] = '已登录';
            $data['createtime'] = date("Y-m-d H:i:s");
            DB::connect('test_pre')->table('xh_siyu_user')->insert($data);
        }
        return ['union_md5' => md5($unionid)];
    }

    /**
     * @author: hao
     * @Description: 测试私域微信登录
     * @Date: 2024-08-17 17:17:55
     */
    public static function getLoginTest($param){
        $data = [];
        $openid = $param['oauth']['openid'];
        $nickname = $param['info']['nickname'];
        $headimgurl = $param['info']['headimgurl'];
        $unionid = $param['info']['unionid'];

        //信息入库
        $where[] = ['unionid', '=', $unionid];
        $where[] = ['del', '=', 0];
        $if_exist = DB::connect('test')->table('xh_siyu_user')->where($where)->find();
        if (empty($if_exist)) {
            $data['openid'] = $openid;
            $data['unionid'] = $unionid;
            $data['union_md5'] = md5($unionid);
            $data['nickname'] = $nickname;
            $data['wx_avatarurl'] = $headimgurl;
            $data['type'] = 0;
            $data['status'] = 1;
            $data['status_ch'] = '已登录';
            $data['createtime'] = date("Y-m-d H:i:s");
            DB::connect('test')->table('xh_siyu_user')->insert($data);
        }
        return ['union_md5' => md5($unionid)];
    }

    /**
     * @author: hao
     * @Description: 正式私域微信登录总表
     * @Date: 2024-08-17 17:17:55
     */
    public static function addUserToall($param, $host='liveapp.banxued.com'){
        $data = [];
        $openid = $param['oauth']['openid'];
        $nickname = $param['info']['nickname'];
        $headimgurl = $param['info']['headimgurl'];
        $unionid = $param['info']['unionid'];

        //信息入库
        $where[] = ['unionid', '=', $unionid];
        $where[] = ['del', '=', 0];
        $if_exist = DB::table('xh_siyu_user_toall')->where($where)->find();
        if (empty($if_exist)) {
            $data['openid'] = $openid;
            $data['unionid'] = $unionid;
            $data['union_md5'] = md5($unionid);
            $data['nickname'] = $nickname;
            $data['wx_avatarurl'] = $headimgurl;
            $data['type'] = 0;
            $data['status'] = 1;
            $data['status_ch'] = '已登录';
            $data['source'] = $host;
            $data['createtime'] = date("Y-m-d H:i:s");
            DB::table('xh_siyu_user_toall')->insert($data);
        }
        return $if_exist;
    }

    /**
     * @author: hao
     * @Description: 正式私域微信登录
     * @Date: 2024-08-17 17:17:55
     */    
    public static function getLoginBuild($param){
        $data = [];
        $openid = $param['oauth']['openid'];
        $nickname = $param['info']['nickname'];
        $headimgurl = $param['info']['headimgurl'];
        $unionid = $param['info']['unionid'];
        $all = self::addUserToall($param);

        //信息入库
        $where[] = ['unionid', '=', $unionid];
        $where[] = ['del', '=', 0];
        $if_exist = DB::table('xh_siyu_user')->where($where)->find();
        if (empty($if_exist)) {
            $data['openid'] = $openid;
            $data['unionid'] = $unionid;
            $data['union_md5'] = md5($unionid);
            $data['nickname'] = $nickname;
            $data['wx_avatarurl'] = $headimgurl;
            $data['type'] = 0;
            $data['status'] = 1;
            $data['status_ch'] = '已登录';
            $data['createtime'] = date("Y-m-d H:i:s");
            if (!empty($all)) {
                $data['phone'] = $all['phone'];
            }
            DB::table('xh_siyu_user')->insert($data);
        }
        return ['union_md5' => md5($unionid)];
    }

    /**
     * @author: hao
     * @Description: 正式私域微信登录-客户-AJ
     * @Date: 2024-08-17 17:17:55
     */   
    public static function getLoginClientAj($param){
        $data = [];
        $openid = $param['oauth']['openid'];
        $nickname = $param['info']['nickname'];
        $headimgurl = $param['info']['headimgurl'];
        $unionid = $param['info']['unionid'];
        $all = self::addUserToall($param, 'ajliveapp.banxued.com');

        //信息入库
        $where[] = ['unionid', '=', $unionid];
        $where[] = ['del', '=', 0];
        $if_exist = DB::connect('client_aj')->table('xh_siyu_user')->where($where)->find();
        if (empty($if_exist)) {
            $data['openid'] = $openid;
            $data['unionid'] = $unionid;
            $data['union_md5'] = md5($unionid);
            $data['nickname'] = $nickname;
            $data['wx_avatarurl'] = $headimgurl;
            $data['type'] = 0;
            $data['status'] = 1;
            $data['status_ch'] = '已登录';
            $data['createtime'] = date("Y-m-d H:i:s");
            if (empty($all)) {
                $data['phone'] = $all['phone'];
            }
            DB::connect('client_aj')->table('xh_siyu_user')->insert($data);
        }
        return ['union_md5' => md5($unionid)];
    }

    /**
     * @author: hao
     * @Description: 正式私域微信登录-客户-李一舟
     * @Date: 2024-08-17 17:17:55
     */
    public static function getLoginClientLiyz($param){
        $data = [];
        $openid = $param['oauth']['openid'];
        $nickname = $param['info']['nickname'];
        $headimgurl = $param['info']['headimgurl'];
        $unionid = $param['info']['unionid'];
        $all = self::addUserToall($param, 'lyzliveapp.banxued.com');

        //信息入库
        $where[] = ['unionid', '=', $unionid];
        $where[] = ['del', '=', 0];
        $if_exist = DB::connect('client_lyz')->table('xh_siyu_user')->where($where)->find();
        if (empty($if_exist)) {
            $data['openid'] = $openid;
            $data['unionid'] = $unionid;
            $data['union_md5'] = md5($unionid);
            $data['nickname'] = $nickname;
            $data['wx_avatarurl'] = $headimgurl;
            $data['type'] = 0;
            $data['status'] = 1;
            $data['status_ch'] = '已登录';
            $data['createtime'] = date("Y-m-d H:i:s");
            if (empty($all)) {
                $data['phone'] = $all['phone'];
            }
            DB::connect('client_lyz')->table('xh_siyu_user')->insert($data);
        }
        return ['union_md5' => md5($unionid)];
    }
    
    
    
}